//====================================================================================================
// Filename:	WinMain.cpp
// Created by:	Peter Chan
// Description:	Hello Animation demo.
//====================================================================================================

//====================================================================================================
// Includes
//====================================================================================================

#include <SGE.h>
using namespace SGE;

#include "Character.h"
#include "Map.h"

//====================================================================================================
// Globals
//====================================================================================================

std::vector<PlaybackSystem*> gPlaybackSystems;
std::vector<Character*> gCharacters;
Map gMap;

SGE_Font gFont;

bool gQuit = false;

//====================================================================================================
// SGE Functions
//====================================================================================================

void SGE_Initialize()
{
	gCharacters.push_back(new Character());
	gCharacters.back()->Load();
	gCharacters.back()->SetPosition(SVector2(100.0f, 100.0f));

	gPlaybackSystems.push_back(new PlaybackSystem());
	gPlaybackSystems.back()->Reset(true);

	gCharacters.back()->SetPlaybackSystem(gPlaybackSystems.back());

	gMap.Load("level01.txt", "texturepack01.txt");

	gFont.Load(24);
}

//----------------------------------------------------------------------------------------------------

void SGE_Terminate()
{
	for (int i = 0; i < (int)gCharacters.size(); ++i)
	{
		gCharacters[i]->Unload();
		delete gCharacters[i];
	}
	gCharacters.clear();

	for (int i = 0; i < (int)gPlaybackSystems.size(); ++i)
	{
		delete gPlaybackSystems[i];
	}
	gPlaybackSystems.clear();

	gMap.Unload();
	gFont.Unload();
}

//----------------------------------------------------------------------------------------------------

bool SGE_Update(float deltaTime)
{
	for (int i = 0; i < (int)gCharacters.size(); ++i)
	{
		gCharacters[i]->Update(deltaTime, gMap);
	}

	gMap.Update(deltaTime);

	// Toggle playback
	if (Input_IsKeyPressed(Keys::ENTER))
	{
		// Start playback
		for (int i = 0; i < (int)gCharacters.size(); ++i)
		{
			gCharacters[i]->SetPosition(SVector2(100.0f, 100.0f));
			gPlaybackSystems[i]->Reset(false);
		}
		
		// Spawn new player
		gCharacters.push_back(new Character());
		gCharacters.back()->Load();
		gCharacters.back()->SetPosition(SVector2(100.0f, 100.0f));

		gPlaybackSystems.push_back(new PlaybackSystem());
		gPlaybackSystems.back()->Reset(true);

		gCharacters.back()->SetPlaybackSystem(gPlaybackSystems.back());
	}

	// Are we done?
	if (Input_IsKeyPressed(Keys::ESCAPE))
	{
		gQuit = true;
	}
	return gQuit;
}

//----------------------------------------------------------------------------------------------------

void SGE_Render()
{
	const SVector2 kTarget = gCharacters.back()->GetPosition();
	const int kScreenWidth = IniFile_GetInt("WinWidth", 800);
	const int kScreenHeight = IniFile_GetInt("WinHeight", 600);
	const int kMapWidth = gMap.GetWidth();
	const int kMapHeight = gMap.GetHeight();

	SVector2 offset;
	offset.x = (kScreenWidth * 0.5f) - kTarget.x;
	offset.x = Clamp(offset.x, (float)kScreenWidth - kMapWidth, 0.0f);
	offset.y = (kScreenHeight * 0.5f) - kTarget.y;
	offset.y = Clamp(offset.y, (float)kScreenHeight - kMapHeight, 0.0f);

	gMap.Render(offset);
	
	for (int i = 0; i < (int)gCharacters.size(); ++i)
	{
		gCharacters[i]->Render(offset);
	}

	gFont.Print(gCharacters.back()->GetCurrentState(), 0, 30);
}
